/* 储存客户列表先关api */

import { ajaxService } from '@/utils/request';
import { getToken } from '@/utils/auth';
import axios from 'axios';
import { PutClientState, ClientAddInfo, PutClientSales } from '../type';

const env = import.meta.env;
// let baseURL = env.VITE_API_BASE_URL;
let baseURL = env.VITE_CLIENT_URL;

// 新增客户
export function PostClientAdd(data: ClientAddInfo) {
  return ajaxService({
    url: '/client/customer',
    method: 'post',
    baseURL,
    data,
  });
}

// 客户列表表头查询
export function GetClientHeader(params: any) {
  return ajaxService({
    url: '/client/customer/header',
    method: 'get',
    baseURL,
    params,
  });
}

// 获取客户列表数据
export function GetClientList(params?: any) {
  return ajaxService({
    url: '/client/page/customer',
    method: 'get',
    baseURL,
    params,
  });
}

// 表头修改
export function PutClientHeader(data: any) {
  return ajaxService({
    url: '/client/customer/header',
    method: 'put',
    baseURL,
    data,
  });
}

// 重新分配销售负责人
export function PutClientSales(data: PutClientSales) {
  return ajaxService({
    url: '/client/customerId',
    method: 'put',
    baseURL,
    data,
  });
}

// 修改客户级别
export function PutClientState(data: PutClientState) {
  return ajaxService({
    url: '/client/customer/state',
    method: 'put',
    baseURL,
    data,
  });
}

//  转黑名单或者解除黑名单
export function PutClient2Black(params: any) {
  return ajaxService({
    url: '/client/black/customer',
    method: 'put',
    baseURL,
    params,
  });
}

// 删除客户列表一条数据
export function DelClientList(params: any) {
  return ajaxService({
    url: `/client/customer`,
    method: 'delete',
    baseURL,
    params,
  });
}

// 获取客户详情
export function GetClientDetail(params: any) {
  return ajaxService({
    url: '/client/customer',
    method: 'get',
    baseURL,
    params,
  });
}

// 获取客户详情对应tabs的数据
export function GetClientDetailTabs(params: any) {
  return ajaxService({
    url: '/client/customer/type',
    method: 'get',
    baseURL,
    params,
  });
}

// 获取表头
export function GetHeaderSettings(params: any) {
  return ajaxService({
    url: '/header/order',
    method: 'get',
    params,
    baseURL,
  });
}

// 设置表头
export function putHeaderSettings(data: any) {
  return ajaxService({
    url: '/header/order',
    method: 'put',
    data,
    baseURL,
  });
}

// 新增跟进记录
export function PostClientDetailRecord(data: any) {
  return ajaxService({
    url: '/client/customer/type/record',
    method: 'post',
    baseURL,
    data,
  });
}

// 删除一条跟进记录
export function DelClientDetailRecord(params: any) {
  return ajaxService({
    url: '/client/customer/type/record',
    method: 'delete',
    baseURL,
    params,
  });
}

// 新增或修改客户需求信息与决策流程
export function PostClientDetailDecision(data: any) {
  return ajaxService({
    url: '/client/customer/type/decision',
    method: 'post',
    baseURL,
    data,
  });
}

// 删除客户需求信息与决策流程
export function DelClientDetailDecision(data: any) {
  return ajaxService({
    url: '/client/customer/type/decision',
    method: 'delete',
    baseURL,
    data,
  });
}

// 新增客户联系人
export function PostClientDetailContact(data: any) {
  return ajaxService({
    url: '/client/customer/type/contact',
    method: 'post',
    baseURL,
    data,
  });
}

// 修改客户联系人
export function PutClientDetailContact(data: any) {
  return ajaxService({
    url: '/client/customer/type/contact',
    method: 'put',
    baseURL,
    data,
  });
}

// 删除客户联系人
export function DelClientDetailContact(data: any) {
  return ajaxService({
    url: '/client/customer/type/contact',
    method: 'delete',
    baseURL,
    data,
  });
}

// 新增客户联系人
export function PostClientDetailEntry(data: any) {
  return ajaxService({
    url: '/client/customer/type/entry',
    method: 'post',
    baseURL,
    data,
  });
}

// 查询单个联系人
export function getClientDetailEntry(params: { id: number }) {
  return ajaxService({
    url: '/client/customer/type/contact',
    method: 'get',
    baseURL,
    params,
  });
}

// 修改客户联系人
export function PutClientDetailEntry(data: any) {
  return ajaxService({
    url: '/client/customer/type/entry',
    method: 'put',
    baseURL,
    data,
  });
}

// 删除客户联系人
export function DelClientDetailEntry(data: any) {
  return ajaxService({
    url: '/client/customer/type/entry',
    method: 'delete',
    baseURL,
    data,
  });
}

// 获取黑名单列表数据
export function GetBlackClientList(params?: any) {
  return ajaxService({
    url: '/client/page/black/customer',
    method: 'get',
    baseURL,
    params,
  });
}

// 编辑客户信息
export function putClientDetail(data?: any) {
  return ajaxService({
    url: '/client/customer/type/base',
    method: 'put',
    baseURL,
    data,
  });
}

// 新增和编辑业务员
export function postClientSalesman(data?: any) {
  return ajaxService({
    url: '/client/customer/salesman',
    method: 'post',
    baseURL,
    data,
  });
}

// 删除业务员
export function delClientSalesman(params?: any) {
  return ajaxService({
    url: '/client/customer/salesman',
    method: 'delete',
    baseURL,
    params,
  });
}

// 导出客户数据
export async function GetClientDeriveExcel(name?: string) {
  const res: any = await axios({
    method: 'get',
    url: '/client/customer/derive',
    baseURL,
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8',
      'Authorization': `${getToken()}`,
    },
    responseType: 'blob',
    // data: file,
  });
  const file = new Blob([res.data], { type: 'application/xlsx' });
  const changeUrl = window.webkitURL.createObjectURL(file);
  let downloadElement: any = document.createElement('a');
  downloadElement.download = (name || 'test') + '.xlsx';
  downloadElement.href = changeUrl;
  downloadElement.click();
  downloadElement = undefined;
  window.URL.revokeObjectURL(changeUrl);
}

// 导出黑名单数据
export async function GetClientBlacklistDeriveExcel(name?: string) {
  const res: any = await axios({
    method: 'get',
    url: '/client/blackList/derive',
    baseURL,
    headers: {
      'Content-Type': 'multipart/form-data;charset=UTF-8',
      'Authorization': `${getToken()}`,
    },
    responseType: 'blob',
    // data: file,
  });
  const file = new Blob([res.data], { type: 'application/xlsx' });
  const changeUrl = window.webkitURL.createObjectURL(file);
  let downloadElement: any = document.createElement('a');
  downloadElement.download = (name || 'test') + '.xlsx';
  downloadElement.href = changeUrl;
  downloadElement.click();
  downloadElement = undefined;
  window.URL.revokeObjectURL(changeUrl);
}
